import { createRouter, createWebHashHistory } from "vue-router";
import { constantRoute } from "./routes.ts";
import setting from "../setting.ts";
import useUserStore from "@/store/modules/user";
import { user } from "@/api/index";

const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoute,
  // 滚动行为
  scrollBehavior(to) {
    if (
      [
        "视频搜索",
        "兑换中心",
        "账号设置",
      ].includes(to.meta.title)
    ) {
      return {
        left: 0,
        top: 730,
      };
    } if (
      [
        "视频播放3"
      ].includes(to.meta.title)
    ) {
      return {
        left: 0,
        top: 530,
      };
    } else {
      return {
        left: 0,
        top: 0,
      };
    }
  },
});

router.beforeEach(async (to: any, _: any, next: any) => {
  document.title = `${setting.title} - ${to.meta.title}`;
  const token = sessionStorage.getItem("is_front_login");
  let userStore = useUserStore();

  //判断当前的token是否存在，也就是登录时的token
  if (token) {
    //如果指向的是登录页面，不做任何操作
    if (to.path === "/login") {
    } else {
      //如果不是登录页面，且token存在，就放行
      await userStore.get_user_info();
      next();
    }
  } else {
    next();
  }
});

export default router;
